package com.yangff.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangff.entity.product.entity.Financialproduct;
import com.yangff.entity.product.entity.dto.AutoStockDTO;

import com.yangff.entity.product.entity.dto.FinancialproductDTO;
import com.yangff.entity.product.entity.productentity.StockType;
import com.yangff.entity.product.entity.vo.ProductVo;
import com.yangff.entity.user.entity.User;
import com.yangff.mapper.FinancialproductMapper;
import com.yangff.service.IFinancialproductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangff.utils.StockUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 * 存储各种理财产品的详细信息 服务实现类
 * </p>
 *
 * @author yangff
 * @since 2025-01-15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FinancialproductServiceImpl extends ServiceImpl<FinancialproductMapper, Financialproduct> implements IFinancialproductService {
    private final FinancialproductMapper financialproductMapper;


    @Override
    public Financialproduct CreateRandomStock(AutoStockDTO stockDTO) {
        log.info("StockDTO:{}", stockDTO);
        Financialproduct product = StockUtil.CreateStockByType(stockDTO.getType());
        product.setName(stockDTO.getName());
        product.setCode(stockDTO.getCode());
        product.setDescription(stockDTO.getDescription());
        //方便起见，最小投资统一十块
        product.setMinInvestment(10);
        return product;
    }

    @Override
    public Financialproduct updateFinancialproductByCode(@RequestBody FinancialproductDTO financialproductDTO)
    {
        //不允许中途修改回报率以及波动率
        Financialproduct financialproduct = getOne(new LambdaQueryWrapper<Financialproduct>().eq(Financialproduct::getCode,financialproductDTO.getCode()));
        if(financialproductDTO.getType() != null)financialproduct.setType(financialproductDTO.getType());
        if(financialproductDTO.getName() != null)financialproduct.setName(financialproductDTO.getName());
        if(financialproductDTO.getDescription() != null)financialproduct.setDescription(financialproductDTO.getDescription());
        if(financialproductDTO.getMinInvestment() != null) financialproduct.setMinInvestment(financialproductDTO.getMinInvestment());
        return financialproduct;

    }

    public IPage<Financialproduct> selectByTypeWithFundnetvalue(Page<Financialproduct> page, String type) {
        // 获取分页结果
        List<Financialproduct> records = financialproductMapper.selectByTypeWithFundnetvalue(page.offset(),page.getSize(), type);

        // 获取总记录数
        Long total = financialproductMapper.countByTypeWithFundnetvalue(type);

        // 设置分页对象的属性
        page.setRecords(records);
        page.setTotal(total);

        return page;
    }

    @Override
    public Page<ProductVo> getProductList(Page<Object> of, String valueOf) {
        // 创建 MyBatis-Plus 的 Page 对象
        Page<Financialproduct> page = new Page<>(of.getCurrent(), of.getSize());

        // 使用自定义的分页查询
        IPage<Financialproduct> productPage = selectByTypeWithFundnetvalue(page, valueOf);
        // 将 Financialproduct 转换为 ProductVo
        Page<ProductVo> productVoPage = new Page<>(productPage.getCurrent(), productPage.getSize(), productPage.getTotal());
        productVoPage.setRecords(BeanUtil.copyToList(productPage.getRecords(), ProductVo.class));

        return productVoPage;
    }
}
